#include <cstdarg>
#include <cassert>
#include <iostream>
#include <stdexcept>

using namespace std;

void error(const char* fmt...)
{
    assert(fmt);
    va_list p;
    va_start(p,fmt);
    for(char const* s = fmt; *s; ++s){
        if(*s != '%'){
            cerr.put(*s);
        }
        else{
            switch(*(++s)){
            case '%':cerr.put('%');
                break;
            case 's':
                cerr << va_arg(p, char const*);
                break;
            case 'c':
                cerr << va_arg(p, char);
                break;
            case 'd':
                cerr << va_arg(p, int);
                break;
            default:
                throw std::domain_error(std::string("panic"));
            }
            va_end(p);
        }
    }
}

            
int main()
{
    int i = 100;
    char c = 'c';
    
    error("%d \n",i);
    error("%s \n","This is a string");
    error("%c",c);
    
}
